package com.infore.airsupervise.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TableLayout;
import android.widget.TextView;

import com.beardedhen.androidbootstrap.AwesomeTextView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.infore.airsupervise.R;
import com.infore.airsupervise.app.Constants;
import com.infore.airsupervise.app.StatusConst;
import com.infore.airsupervise.bean.LawingSiteE;
import com.infore.airsupervise.other.listener.LayoutControl;
import com.infore.airsupervise.other.listener.LayoutControlListener;
import com.infore.airsupervise.ui.fragment.MissionFileShowFragment;
import com.infore.airsupervise.ui.fragment.ShowPhotoFragment;
import com.infore.airsupervise.ui.widget.ItemDecoration1px;
import com.infore.airsupervise.utils.JumpUtil;
import com.infore.airsupervise.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 现场执法详情主界面
 * Created by xiaotao on 2017/5/26.
 */

public class SiteMainDetailActivity extends BaseTitleBarActivity {
    @BindView(R.id.tv_company_title)
    TextView tvCompanyTitle;
    @BindView(R.id.tv_check_time)
    TextView tvCheckTime;
    @BindView(R.id.tv_record_arrow)
    AwesomeTextView tvRecordArrow;
    @BindView(R.id.rv_record)
    RecyclerView rvRecord;
    @BindView(R.id.tv_evidence_arrow)
    AwesomeTextView tvEvidenceArrow;
    @BindView(R.id.tv_signature_arrow)
    AwesomeTextView tvSignatureArrow;
    @BindView(R.id.ll_signature)
    TableLayout llSignature;
    @BindView(R.id.fl_files)
    FrameLayout flFiles;


    private LayoutControlListener layoutControl; //布局显示隐藏
    private ShowPhotoFragment comSignFra;
    private ShowPhotoFragment inspectSignFra;
    private MissionFileShowFragment fileFra;

    private LawingSiteE lawingSiteE;

    private List<LawingSiteE.RecordFormat> recordList = new ArrayList<>();
    private BaseQuickAdapter<LawingSiteE.RecordFormat,BaseViewHolder> recordAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sitelawing_detail);
        ButterKnife.bind(this);
        Intent it = getIntent();
        if (null != it) {
            lawingSiteE = (LawingSiteE) it.getSerializableExtra(Constants.INTENT_OBJ_KEY);
        }
        layoutControl = new LayoutControl(ctx);
        initView();
        initRecyclerView();
    }

    /**
     * 初始化控件 以及设置台账数据
     */
    private void initView() {
        //titlebar
        rightMenu.setVisibility(View.INVISIBLE);
        if (null == lawingSiteE) {
            ToastUtil.showShort(ctx, "参数传递错误");
            return;
        }
        title.setText(lawingSiteE.getName());
        /*
         *基本信息
         */
        tvCompanyTitle.setText(lawingSiteE.getName());
        tvCheckTime.setText(lawingSiteE.getTime());
        /*
         * 企业人员签名
         */
        if (null != lawingSiteE.getSignatureCom() &&
                lawingSiteE.getSignatureCom().size() > 0) {
            comSignFra = new ShowPhotoFragment(lawingSiteE.getSignatureCom(), 200);
            getSupportFragmentManager().beginTransaction().
                    replace(R.id.fl_singatrue_com, comSignFra).commit();
        }
        /*
         *   执法人员签名
         */
        if (null != lawingSiteE.getSignaturePson() &&
                lawingSiteE.getSignaturePson().size() > 0) {
            inspectSignFra = new ShowPhotoFragment(lawingSiteE.getSignaturePson(), 200);
            getSupportFragmentManager().beginTransaction().
                    replace(R.id.fl_singatrue_inspect, inspectSignFra).commit();
        }
        /*
         *执法取证附件
         */
        if(null != lawingSiteE.getEvidence()) {
            fileFra = new MissionFileShowFragment(lawingSiteE.getEvidence().getFiles());
            getSupportFragmentManager().beginTransaction().
                        replace(R.id.fl_files, fileFra).commit();

        }

    }

    /**
     * 记录，笔录 列表展示
     */
    private void initRecyclerView(){
        getRecordDatas();
        //适配器
        recordAdapter = new BaseQuickAdapter<LawingSiteE.RecordFormat, BaseViewHolder>(R.layout.item_mission_mune,recordList) {
            @Override
            protected void convert(BaseViewHolder helper, LawingSiteE.RecordFormat item) {
                        helper.setText(R.id.tv_mission_menu,StatusConst.modelTypeTransfer(item.getType()));
            }
        };
        //recyclerView
        rvRecord.setLayoutManager( new LinearLayoutManager(ctx));
        ItemDecoration1px itemDecoration = new ItemDecoration1px(ctx , ItemDecoration1px.VERTICAL_LIST);
        rvRecord.addItemDecoration(itemDecoration);
        rvRecord.setAdapter(recordAdapter);
        rvRecord.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                LawingSiteE.RecordFormat record = recordList.get(position);
                JumpUtil.jumpFromTo(ctx,SitePartDetailActivity.class,Constants.INTENT_OBJ_KEY,record);

            }
        });
    }

    /**
     * 将记录，笔录转换成格式化对象
     */
    private void getRecordDatas(){
        /*
         * 检查记录
         */
        List<LawingSiteE.RecorderJC> recorderJCs = lawingSiteE.getRecorderJCList();
        if(null != recorderJCs && recorderJCs.size()>0){
            for(LawingSiteE.RecorderJC e : recorderJCs ){
                LawingSiteE.RecordFormat recordFormat =
                        new LawingSiteE.RecordFormat(e.getId(), StatusConst.SITE_LEDGER_CHECK);
                recordList.add(recordFormat);
            }
        }
        /*
         *  检查笔录
         */
        List<LawingSiteE.NotesJC> notesJCs = lawingSiteE.getNotesJCList();
        if(null != notesJCs && notesJCs.size() > 0){
            for(LawingSiteE.NotesJC e : notesJCs){
                LawingSiteE.RecordFormat recordFormat =
                        new LawingSiteE.RecordFormat(e.getId(), StatusConst.SITE_LEDGER_INSPECT);
                recordList.add(recordFormat);
            }
        }
        /*
         * 询问笔录
         */
        List<LawingSiteE.NotesAsk> askList = lawingSiteE.getNotesAskList();
        if(null != askList && askList.size() > 0){
            for(LawingSiteE.NotesAsk e : askList){
                LawingSiteE.RecordFormat recordFormat =
                        new LawingSiteE.RecordFormat(e.getId(), StatusConst.SITE_LEDGER_ASK);
                recordList.add(recordFormat);
            }
        }
    }


    private boolean isRecordShow;
    private boolean isEvidenceShow;
    private boolean isSignatrueShow;

    @OnClick({R.id.ll_record_control, R.id.ll_evidence_control, R.id.ll_signature_control})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_record_control: //记录布局
                isRecordShow = !isRecordShow;
                layoutControl.layoutControl(isRecordShow, rvRecord, tvRecordArrow);
                break;
            case R.id.ll_evidence_control: //取证附件
                isEvidenceShow = !isEvidenceShow;
                layoutControl.layoutControl(isEvidenceShow, flFiles, tvEvidenceArrow);
                break;
            case R.id.ll_signature_control: //签名布局
                isSignatrueShow = !isSignatrueShow;
                layoutControl.layoutControl(isSignatrueShow, llSignature, tvSignatureArrow);
                break;
        }
    }

    @OnClick(R.id.ll_signature_control)
    public void onViewClicked() {
    }



}
